//
//  YWContactHeadFooterView.m
//  maintain
//
//  Created by Kyo on 16/5/5.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWContactHeadFooterView.h"
#import "YWMacro.h"

#import "YWOrganization.h"
#import "YWDepartment.h"
#import "YWUploadFailedItem.h"

@interface YWContactHeadFooterView()
@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)UILabel *lab;
@end

@implementation YWContactHeadFooterView

+ (instancetype)headerWithTableView:(UITableView *)tableView{
    
    static NSString* HeaderIdentifier = @"YWContactHeadFooterViewIndentifier";
    
    YWContactHeadFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[YWContactHeadFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
    }
    
    return headerView;
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier])
    {
        //创建一个按钮
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        //设置按钮的属性
        //设置普通状态下按钮的背景图片
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        //设置高亮状态下按钮的背景图片
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];

        //设置按钮上的小三角图片
        [btn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        //设置按钮上信息的对其方式为左对齐
        btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
        //设置小三角图片的内边距
        btn.contentEdgeInsets=UIEdgeInsetsMake(0, 20, 0, 0);
        //设置按钮上文字距离小三角图片的距离
        btn.titleEdgeInsets=UIEdgeInsetsMake(0, 20, 0, 70);
        //设置按钮上分组标题的文本颜色（默认是白色）
        //[btn setTintColor:[UIColor blackColor]];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //添加按钮的点击事件
        [btn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];

        // 设置btn中的图片不填充整个imageview
        btn.imageView.contentMode = UIViewContentModeCenter;
        // 超出范围的图片不要剪切
        // btn.imageView.clipsToBounds = NO;
        btn.imageView.layer.masksToBounds = NO;

        //把按钮添加到视图
        [self.contentView addSubview:btn];
        self.btn=btn;

        //创建一个lab
        UILabel *lab=[[UILabel alloc]init];
        //设置在线人数的对齐方式为右对齐
        lab.textAlignment=NSTextAlignmentRight;
        //设置在线人数的文本颜色为灰色
        lab.textColor=[UIColor grayColor];
        [self.contentView addSubview:lab];
        self.lab=lab;
    }
    return self;
}

- (void)btnOnclick:(UIButton *)btn
{
    //修改模型的isopen属性
    self.department.isOpen = !self.department.isOpen;
    self.uploadFailedItem.isOpen = !self.uploadFailedItem.isOpen;
    
    //通知代理
    if ([self.delegate respondsToSelector:@selector(clickHeaderView:)]) {
        [self.delegate clickHeaderView:self];
    }
}

 //当控件的frame值改变时，会自动调用该方法，故可以在该方法中设置控件的frame;
- (void)layoutSubviews
{
    [super layoutSubviews];
    //设置按钮的frame和头部视图一样大小
    self.btn.frame=self.bounds;

    //设置lab的frame
    CGFloat padding=20;
    CGFloat labW=50;
    CGFloat labH=self.frame.size.height;
    CGFloat labY=0;
    CGFloat labX=self.frame.size.width-padding-labW;
    self.lab.frame=CGRectMake(labX, labY, labW, labH);
 }

#pragma mark - didMoveToSuperview willMoveToSuperview
 // 已经被添加到父视图上的时候会调用
- (void)didMoveToSuperview
{
//    YWLog(@"已经添加到视图了");
    // 在这个方法中就快要拿到最新的被添加到tableview上的头部视图修改它的图片
     //让小三角图片向下旋转
    [UIView animateWithDuration:0.3 animations:^{
        BOOL isOpen = _department ? _department.isOpen : _uploadFailedItem.isOpen;
        self.btn.imageView.transform = isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
    }];
}

// // 即将被添加到父视图上的时候会调用
//- (void)willMoveToSuperview:(UIView *)newSuperview
//{
//    YWLog(@"将要添加到视图了");
//}
- (void)setDepartment:(YWDepartment *)department
 {
    _department = department;
     NSString *title = _showOrganizationName ? [NSString stringWithFormat:@"%@%@",_department.organization.organizationName?:@"",_department.departmentName?:@""] : _department.departmentName?:@"";
    //设置分组标题
    [self.btn setTitle:title forState:UIControlStateNormal];
    //设置人数
    self.lab.text=[NSString stringWithFormat:@"%lu",(unsigned long)_department.memberArray.count];
 }

- (void)setUploadFailedItem:(YWUploadFailedItem *)uploadFailedItem{
    _uploadFailedItem = uploadFailedItem;
    //设置分组标题
    [self.btn setTitle:_uploadFailedItem.itemName forState:UIControlStateNormal];
    //设置数量
    self.lab.text=[NSString stringWithFormat:@"%lu",(unsigned long)_uploadFailedItem.itemArray.count];
}
@end
